package com.longqinuo.mycarservice.utils;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.longqinuo.mycarservice.BuildConfig;
import com.longqinuo.mycarservice.R;
import com.longqinuo.mycarservice.fragments.FindPasswordFragment;


import org.json.JSONException;
import org.json.JSONObject;



/**
 * Created by zxw on 2016/6/8.
 */
public class MyTextWatcher implements TextWatcher {
    private CharSequence temp;
    private int editStart ;
    private int editEnd ;

    private EditText mEditText;
    private String mPhone;
    public MyTextWatcher(EditText et,String phone) {
        this.mEditText = et;
        this.mPhone = phone;
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        temp = s;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        editStart = mEditText.getSelectionStart();
        editEnd = mEditText.getSelectionEnd();
        switch (mEditText.getId()) {
            case R.id.et_phone_number_find:
                if (temp.length() == 11) {
                    String phone = mEditText.getText().toString().trim();
                    RequestQueue queue = Volley.newRequestQueue(mEditText.getContext());
                    String hasUrl = PathUtils.IS_HASED + "?phone=" + phone;
                    if (BuildConfig.DEBUG) Log.d("FindPasswordFragment", phone);
                    JsonObjectRequest r = new JsonObjectRequest(hasUrl, null, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            if (BuildConfig.DEBUG) Log.d(">>>>>>", "日志"+jsonObject.toString());
                            try {
                                int code = jsonObject.getInt("code");
                                if (code == 0 ) {
                                    Toast.makeText(mEditText.getContext(),
                                            "该手机号已经注册，可以找回密码", Toast.LENGTH_SHORT)
                                            .show();
                                    FindPasswordFragment.setHas(true);
                                } else {
                                    Toast.makeText(mEditText.getContext(),
                                            "该手机号还未注册!", Toast.LENGTH_SHORT)
                                            .show();
                                    FindPasswordFragment.setHas(false);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {

                        }
                    });
                    queue.add(r);
                }
                break;
            default:
                if (temp.length() == 4) {
                    SMSSDK.submitVerificationCode("86", mPhone, mEditText.getText().toString());//提交验证码
                }
                if (temp.length() > 4) {
                    Toast.makeText(mEditText.getContext(),
                            "你输入的字数已经超过了限制！", Toast.LENGTH_SHORT)
                            .show();
                    s.delete(editStart-1, editEnd);
                    int tempSelection = editStart;
                    mEditText.setText(s);
                    mEditText.setSelection(tempSelection);

                }

                break;

        }
    }
}
